﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Configuration;

namespace ElCharro01.Manager
{
    public class ImageManager
    {

        public static string STATIC = "static";
        public static string PROPORTIONAL = "proportional";

        public static void CreateImage()
        {

        }

        public static Bitmap Resize(Bitmap image, int width, int height)
        {

            int newWidth = 0;
            int newHeight = 0;

            
                float srcAspectRatio = (float)image.Width / (float)image.Height;
                float dstAspectRatio = (float)width / (float)height;

                if (srcAspectRatio < dstAspectRatio)
                {
                    newWidth = width;
                    newHeight = (int)Math.Ceiling(image.Height * width / (float)image.Width);
                }
                else
                {
                    newHeight = height;
                    newWidth = (int)Math.Ceiling(image.Width * height / (float)image.Height);
                }
            
      
            Bitmap thumbnailBitmap = new Bitmap(newWidth, newHeight);

            Graphics thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
            thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
            thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
            thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

            Rectangle imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
            thumbnailGraph.DrawImage(image, imageRectangle);

            return thumbnailBitmap;
        }
        public static Bitmap Crop(Bitmap crop, int imageWidth, int imageHeight)
        {
            int targetX = (crop.Width - imageWidth) / 2;
            int targetY = (crop.Height - imageHeight) / 2;

            Bitmap thumbnailBitmap = new Bitmap(imageWidth, imageHeight, PixelFormat.Format24bppRgb);
            thumbnailBitmap.SetResolution(72, 72);

            Graphics grPhoto = Graphics.FromImage(thumbnailBitmap);
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
            grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
            grPhoto.DrawImage(crop,
                              new Rectangle(0, 0, imageWidth, imageHeight),
                              new Rectangle(targetX, targetY, imageWidth, imageHeight),
                              GraphicsUnit.Pixel);

            grPhoto.Dispose();
            crop.Dispose();

            return thumbnailBitmap;
        }
        public static void Delete(string path)
        {
            FileInfo File = new FileInfo(path);
            if (File.Exists)
                File.Delete();
        }
        public static void Save(string path, Bitmap bitmap)
        {

            
            ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
            Encoder myEncoder = Encoder.Quality;
            EncoderParameters myEncoderParameters = new EncoderParameters(1);

            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder,11000000);

            myEncoderParameters.Param[0] = myEncoderParameter;

            bitmap.Save(path, jgpEncoder, myEncoderParameters);
            bitmap.Dispose();
        }
        private static ImageCodecInfo GetEncoder(ImageFormat format)
        {

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }
    }
}